<?php
/**
 * User: MT
 * Date: 2018/5/6 0006
 */

namespace core\lib;
class route
{
    public $action = '';
    public $method = '';

    /*
     * 拆分路由
     * */
    function __construct()
    {
        //1.隐藏index.php-根目录下创建.htaccess
        //2.获得url参数部分
        //不设置虚拟主机
//        [REQUEST_URI] => /local/PHPT/1/1
//        [SCRIPT_NAME] => /local/PHPT/index.php
//        [PHP_SELF] => /local/PHPT/index.php
        //3.返回对应控制器和方法
        //设置虚拟主机
//        [REQUEST_URI] => /1/1
//        [SCRIPT_NAME] => /index.php
//        [PHP_SELF] => /index.php
        //echo $_SERVER['REQUEST_URI'];exit;
        if ($_SERVER['REQUEST_URI'] && $_SERVER['REQUEST_URI'] == '/') {
            $this->action = \core\lib\config::get('default_action','route');
            $this->method = \core\lib\config::get('default_method','route');
        } else {
            $request_url_arr = explode('/', trim($_SERVER['REQUEST_URI'], '/'));
            $this->action = $request_url_arr[0];
            unset($request_url_arr[0]);
            if (empty($request_url_arr[1])) {
                $this->method = \core\lib\config::get('default_method','route');
            } else {
                $this->method = $request_url_arr[1];
            }
            unset($request_url_arr[1]);
        }
        if (isset($request_url_arr) && count($request_url_arr) != 0) {
            $count = count($request_url_arr) + 2;
            $i = 2;
            $_GET = [];
            while ($i < $count) {
                if (isset($request_url_arr[$i + 1])) {
                    $_GET[$request_url_arr[$i]] = $request_url_arr[$i + 1];
                }
                $i = $i + 2;
            }
            pp($_GET);
        }
    }
}